import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _isNumber from "lodash/isNumber";
import _isEmpty from "lodash/isEmpty";
import _upperFirst from "lodash/upperFirst";
import { getResponse } from 'utils/utils';
import { queryMenuTree as _queryMenuTree, createMenuDir as _createMenuDir, saveMenuTree as _saveMenuTree, saveMenuDir as _saveMenuDir, deleteMenuTree as _deleteMenuTree, customMenuImport as _customMenuImport, queryMenuTreeByOrganizationId, createMenuDirByOrganizationId, checkMenuDirExistsByOrganizationId, saveMenuTreeByOrganizationId } from "../services/menuService";

function defineProperty(obj, property, value) {
  Object.defineProperty(obj, property, {
    value: value,
    writable: true,
    enumerable: false,
    configurable: true
  });
}

function defineLevel(obj, level) {
  defineProperty(obj, '$level', level);
}

function defineParentName(obj, name) {
  defineProperty(obj, '$parentName', name);
}

function getDataSource(collections) {
  var newLevel = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : -1;
  var parentName = arguments.length > 2 ? arguments[2] : undefined;
  var level = newLevel;
  level++;
  collections.map(function (n) {
    var menu = n;
    var subMenus = menu.subMenus,
        name = menu.name;
    defineLevel(menu, level);

    if (parentName) {
      defineParentName(menu, parentName);
    }

    if (!_isEmpty(menu.subMenus)) {
      getDataSource(subMenus, level, name);
    }

    return menu;
  });
  return collections;
}

function setSort(data) {
  data.forEach(function (value, index) {
    Object.assign(value, {
      sort: index
    });

    if (value.subMenus) {
      setSort(value.subMenus);
    }
  });
  return data;
}

export default {
  namespace: 'menu',
  state: {
    code: {},
    platform: {
      dataSource: [],
      refresh: false
    },
    organization: {
      dataSource: [],
      refresh: false
    },
    project: {
      dataSource: [],
      refresh: false
    },
    user: {
      dataSource: [],
      refresh: false
    }
  },
  effects: {
    queryMenuTree: /*#__PURE__*/_regeneratorRuntime.mark(function queryMenuTree(_ref, _ref2) {
      var activeKey, params, organizationId, put, call, response, dataSource;
      return _regeneratorRuntime.wrap(function queryMenuTree$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              activeKey = _ref.activeKey, params = _ref.params, organizationId = _ref.organizationId;
              put = _ref2.put, call = _ref2.call;

              if (!(_isNumber(organizationId) && organizationId !== 0)) {
                _context.next = 8;
                break;
              }

              _context.next = 5;
              return call(queryMenuTreeByOrganizationId, organizationId, params);

            case 5:
              response = _context.sent;
              _context.next = 11;
              break;

            case 8:
              _context.next = 10;
              return call(_queryMenuTree, params);

            case 10:
              response = _context.sent;

            case 11:
              if (!(response && !response.failed && activeKey)) {
                _context.next = 15;
                break;
              }

              dataSource = getDataSource(response);
              _context.next = 15;
              return put({
                type: "update".concat(_upperFirst(activeKey), "Reducer"),
                payload: {
                  dataSource: dataSource,
                  refresh: false
                }
              });

            case 15:
            case "end":
              return _context.stop();
          }
        }
      }, queryMenuTree);
    }),
    createMenuDir: /*#__PURE__*/_regeneratorRuntime.mark(function createMenuDir(_ref3, _ref4) {
      var _ref3$payload, payload, call, response;

      return _regeneratorRuntime.wrap(function createMenuDir$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _ref3$payload = _ref3.payload, payload = _ref3$payload === void 0 ? {} : _ref3$payload;
              call = _ref4.call;
              _context2.next = 4;
              return call(_isNumber(payload.organizationId) ? createMenuDirByOrganizationId : _createMenuDir, payload);

            case 4:
              response = _context2.sent;
              return _context2.abrupt("return", !_isEmpty(getResponse(response)));

            case 6:
            case "end":
              return _context2.stop();
          }
        }
      }, createMenuDir);
    }),
    saveMenuDir: /*#__PURE__*/_regeneratorRuntime.mark(function saveMenuDir(_ref5, _ref6) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function saveMenuDir$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.next = 4;
              return call(_saveMenuDir, payload);

            case 4:
              response = _context3.sent;
              return _context3.abrupt("return", !_isEmpty(getResponse(response)));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, saveMenuDir);
    }),
    deleteMenuTree: /*#__PURE__*/_regeneratorRuntime.mark(function deleteMenuTree(_ref7, _ref8) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function deleteMenuTree$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context4.next = 4;
              return call(_deleteMenuTree, payload);

            case 4:
              response = _context4.sent;
              return _context4.abrupt("return", response);

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, deleteMenuTree);
    }),
    customMenuImport: /*#__PURE__*/_regeneratorRuntime.mark(function customMenuImport(_ref9, _ref10) {
      var organizationId, params, call, res;
      return _regeneratorRuntime.wrap(function customMenuImport$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              organizationId = _ref9.organizationId, params = _ref9.params;
              call = _ref10.call;
              _context5.next = 4;
              return call(_customMenuImport, organizationId, params);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, customMenuImport);
    }),
    updateTableDataSource: /*#__PURE__*/_regeneratorRuntime.mark(function updateTableDataSource(_ref11, _ref12) {
      var payload, put, activeKey, dataSource;
      return _regeneratorRuntime.wrap(function updateTableDataSource$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref11.payload;
              put = _ref12.put;
              activeKey = payload.activeKey, dataSource = payload.dataSource;
              _context6.next = 5;
              return put({
                type: "update".concat(_upperFirst(activeKey), "Reducer"),
                payload: {
                  dataSource: getDataSource(dataSource)
                }
              });

            case 5:
            case "end":
              return _context6.stop();
          }
        }
      }, updateTableDataSource);
    }),
    checkMenuDirExists: /*#__PURE__*/_regeneratorRuntime.mark(function checkMenuDirExists(_ref13, _ref14) {
      var organizationId, customFlag, params, call, res;
      return _regeneratorRuntime.wrap(function checkMenuDirExists$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              organizationId = _ref13.organizationId, customFlag = _ref13.customFlag, params = _ref13.params;
              call = _ref14.call;
              _context7.next = 4;
              return call(checkMenuDirExistsByOrganizationId, organizationId, customFlag, params);

            case 4:
              res = _context7.sent;
              return _context7.abrupt("return", res);

            case 6:
            case "end":
              return _context7.stop();
          }
        }
      }, checkMenuDirExists);
    }),
    saveMenuTree: /*#__PURE__*/_regeneratorRuntime.mark(function saveMenuTree(_ref15, _ref16) {
      var params, level, organizationId, call, res;
      return _regeneratorRuntime.wrap(function saveMenuTree$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              params = _ref15.params, level = _ref15.level, organizationId = _ref15.organizationId;
              call = _ref16.call;

              if (!(_isNumber(organizationId) && organizationId !== 0)) {
                _context8.next = 8;
                break;
              }

              _context8.next = 5;
              return call(saveMenuTreeByOrganizationId, organizationId, params, level);

            case 5:
              res = _context8.sent;
              _context8.next = 11;
              break;

            case 8:
              _context8.next = 10;
              return call(_saveMenuTree, setSort(params), level);

            case 10:
              res = _context8.sent;

            case 11:
              return _context8.abrupt("return", getResponse(res));

            case 12:
            case "end":
              return _context8.stop();
          }
        }
      }, saveMenuTree);
    })
  },
  reducers: {
    updateStateReducer: function updateStateReducer(state, _ref17) {
      var payload = _ref17.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    },
    updatePlatformReducer: function updatePlatformReducer(state, _ref18) {
      var payload = _ref18.payload;
      return _objectSpread(_objectSpread({}, state), {}, {
        platform: _objectSpread(_objectSpread({}, state.platform), payload)
      });
    },
    updateOrganizationReducer: function updateOrganizationReducer(state, _ref19) {
      var payload = _ref19.payload;
      return _objectSpread(_objectSpread({}, state), {}, {
        organization: _objectSpread(_objectSpread({}, state.organization), payload)
      });
    },
    updateProjectReducer: function updateProjectReducer(state, _ref20) {
      var payload = _ref20.payload;
      return _objectSpread(_objectSpread({}, state), {}, {
        project: _objectSpread(_objectSpread({}, state.project), payload)
      });
    },
    updateUserReducer: function updateUserReducer(state, _ref21) {
      var payload = _ref21.payload;
      return _objectSpread(_objectSpread({}, state), {}, {
        user: _objectSpread(_objectSpread({}, state.user), payload)
      });
    },
    updateRefreshReducer: function updateRefreshReducer(state) {
      return _objectSpread(_objectSpread({}, state), {}, {
        platform: _objectSpread(_objectSpread({}, state.platform), {}, {
          refresh: true
        }),
        project: _objectSpread(_objectSpread({}, state.project), {}, {
          refresh: true
        }),
        user: _objectSpread(_objectSpread({}, state.user), {}, {
          refresh: true
        }),
        organization: _objectSpread(_objectSpread({}, state.organization), {}, {
          refresh: true
        })
      });
    }
  }
};